﻿/**************************************************************
*作者：Leon
*创建时间：2022/4/5 16:25:35
**************************************************************/
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace Lad.DrawProject.Controls;

public class DragThumb : Thumb
{
    private IBaseCanvasViewModel? serviceProvider;

    public IBaseCanvasViewModel ServiceProvider
    {
        get { return serviceProvider??= (IBaseCanvasViewModel)this.GetBaseCanvas()!.DataContext; }
    }

    public DragThumb()
    {
        base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
        base.DragStarted += DragThumb_DragStarted;
        base.DragCompleted += DragThumb_DragCompleted;
    }

    private List<SelectableItemViewModelBase>? designerItems;

    private void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
    {
        if (this.DataContext is SelectableItemViewModelBase designerItem && designerItem.IsSelected)
        {
            // we only move DesignerItems
            designerItems = ServiceProvider.SelectedItems.ToList();

            foreach (var item in designerItems)//设置撤回操作的历史值
            {
                item.SetOldValue(new() {
                    (item.Top, nameof(item.Top)),
                    (item.Left, nameof(item.Left))
                });
            }
            e.Handled = true;
        }
        else
        {
            designerItems = null;
        }
    }

    private void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        if (designerItems != null)
        {
            foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
            {
                item.RaiseTopLeft(); 
            }
            var canvas = (e.OriginalSource as DependencyObject).GetBaseCanvas();
            if (canvas != null)
                canvas.InvalidateMeasure();
            e.Handled = true;
        }
    }

    void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        if (designerItems != null)
        {
            var objs = designerItems.OfType<DesignerItemViewModelBase>().ToList();
            if (objs.Count > 0)
            {
                var minLeft = objs.Min(p => p.Left);
                var minTop = objs.Min(p => p.Top);
                foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
                {
                    double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
                    double deltaVertical = Math.Max(-minTop, e.VerticalChange);
                    item.Left += deltaHorizontal;
                    item.Top += deltaVertical;
                }
            }

            e.Handled = true;
        }
    }

}

public static class ThumbExtention
{
    public static BaseCanvas? GetBaseCanvas(this DependencyObject? element)
    {
        while (element != null && element is not BaseCanvas)
            element = VisualTreeHelper.GetParent(element);

        return element as BaseCanvas;
    }
}
